package com.hlz.rabbit.mq.util;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

/**
 * 工具类
 *
 * @author Hongliang Zhu
 * @create 2022-11-20 15:56
 */
public class RabbitMqUtils {

    public static Channel getChannel() throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.160.200");
        connectionFactory.setUsername("admin");
        connectionFactory.setVirtualHost("my_vhost");
        connectionFactory.setPassword("admin");

        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        return channel;
    }

    public static void console(Channel channel, String exchange, String routingKey) throws IOException {
        Scanner scanner = new Scanner(System.in);

        while (scanner.hasNext()){
            String message = scanner.nextLine();
            channel.basicPublish(exchange, routingKey, null, message.getBytes());
            System.out.println("message send success!");
        }
    }
}
